from flask import Flask,render_template,request,redirect

app = Flask(__name__)

#首页
@app.route("/")
def index():
    return render_template("index.html")

#处理用户登陆的请求
@app.route("/login",methods=['POST','GET'])
def login():
    username = request.form['username']#接受输入的用户名
    userpwd = request.form['userpwd']#将接收到输入的密码
    if username =='admin':
        if userpwd =='admin123':
            return redirect("scrawl")  #跳转到爬虫页面
        else:
            return redirect("/")  #返回登陆页面
    else:
        return redirect("/")  #返回登陆页面

#爬虫的作业：网页端的表单设计
@app.route("/scrawl")
def scrawler():
    return render_template("scrawl.html")

@app.route("/start",methods=['POST','GET'])
def start():
    name = request.form['targetname']
    url = request.form['targeturl']
    #调用爬虫函数开始爬取数据
    return str(crawl(url,selector="p"))

import requests

from bs4 import BeautifulSoup
def crawl(url, selector):
    headers={
        "User - Agent":"Mozilla / 5.0(Windows NT 10.0;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 92.0.4515.131Safari / 537.36SLBrowser / 8.0.1.1171SLBChan / 103"
    }
    #获取源代码
    htmlcodes=requests.get(url,headers=headers).text

    BeautifulSoup(htmlcodes,"html.parser")
    #解析源代码
    bs=BeautifulSoup(htmlcodes,"html.parser")
    #根据选择器返回文本



    return[item.text for item in bs.select(selector=selector)]

if __name__=="__main__":
    app.run()
